package com.hualan.servlet;

import com.alibaba.fastjson.JSON;
import com.hualan.dao.PanghuGoodsDao;
import com.hualan.dao.PanghuGoodsDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
//修改商品的上下架（根据goodsId修改goodsStatus!）
@WebServlet("/goods/status")
public class PanghuGoodsStatusServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PanghuGoodsDao panghuGoodsDao = new PanghuGoodsDaoImpl();
        //获取前端传回的goodsId数据
        String goodsIdStr = req.getParameter("goodsId");
        int goodsId = Integer.parseInt(goodsIdStr);

        //获取前端goodsStatus数据
        String goodsStatusStr = req.getParameter("goodsStatus");
        int goodsStatus = Integer.parseInt(goodsStatusStr);

        //调用SQL语句进行修改goodsStatus
        int row = panghuGoodsDao.modifyGoodsStatus(goodsStatus,goodsId);

        System.out.println("!!!!!!!!"+goodsId+"_____________"+goodsStatus);

        //存放返回前端的数据
        Map<String, Object> map = new HashMap<>();
        //反馈修改是否成功
        if (row < 1){
            map.put("status", 302);
            map.put("message", "修改失败！");
        }else {
            map.put("status", 200);
            map.put("message", "修改成功！");
        }
        resp.getWriter().write(JSON.toJSONString(map));
    }
}
